const express = require('express')
const fs = require('fs')


const userslist = [
	{ id: 500, role_name: 'super admin', username: 'sudo', tel: '123', role_status: true},
	{ id: 501, role_name: 'admin', username: 'adam', tel: '321', role_status: false},
	{ id: 502, role_name: 'admin', username: 'linken', tel: '777', role_status: false},
	{ id: 503, role_name: 'admin', username: 'shwa', tel: '877', role_status: false},
	{ id: 504, role_name: 'admin', username: 'jj', tel: '787', role_status: false},
	{ id: 505, role_name: 'admin', username: 'jackey', tel: '778', role_status: false},
	{ id: 506, role_name: 'admin', username: 'shampoo', tel: '7877', role_status: false},
	{ id: 507, role_name: 'admin', username: 'lora', tel: '7787', role_status: false}
]

const matchUser = function(str) {
	return userslist.filter(item => (new RegExp(str, 'g')).test(item.username))
}

var app = express.Router()

app.get('/', (req, res) => {
	res.redirect('/login')
})

app.get('/login', (req, res) => {
	res.send('fuck')
})

app.get('/fetch', (req, res) => {
	let id = req.query.id - 0
	const item = userslist.find(it => it.id == id)
	res.status(200).send({ data: item, status: '200'})
})

app.get('/edit', (req, res) => {
	let { id, tel } = req.query
	// console.log(id, tel)
	const index = userslist.findIndex(it => it.id == id - 0)
	userslist[index].tel = tel
	res.status(200).send({ data: userslist[index], status: '200'})
})

app.post('/login', (req, res) => {
	// 与数据库内容匹配
	let { username : usrname, password : passwd } = req.body
	if (usrname != 'admin' || passwd != '123456') {
		return res.status(200).send({ status: '400' })
	}
	res.status(200).send({ status: '200', token: 'wsxkcialoqlMxZm' })	
})

app.get('/menus', (req, res) => {
	fs.readFile('./booklist.json', 'utf8', (err, str) => {
		if (err) {
			return res.status(200).send({ status: '400' })
		}
		let info = JSON.parse(str)
		info.status = '200'
		let ret = JSON.stringify(info)
		res.status(200).send(ret)
	})
})

app.get('/users', (req, res) => {
	let {
		query,
		pagenum,
		pagesize
	} = req.query
	
	let curIndexStart = pagenum - 1 ? (pagenum - 1) * pagesize : 0
	let curIndexEnd = curIndexStart + (pagesize - 0)
	let users = [], total = userslist.length
	if (query !== '') {
		const matchret = matchUser(query)
		total = matchret.length
		users = matchret.slice(curIndexStart, curIndexEnd)
	} else {
		users = userslist.slice(curIndexStart, curIndexEnd)
	}
	let resobj = {
		data: {
			pagenum,
			total,
			users
		},
		status: '200'
	}
	// console.log(pagenum, pagesize)
	// console.log(curIndexStart, curIndexEnd)
	res.status(200).send(resobj)
})

app.get('/userstate', (req, res) => {
	let { id, role_status } = req.query
	let index = userslist.findIndex(item => item.id == id - 0)
	if (index == -1) {
		return res.send({ data: '', status: '500'})
	}
	userslist[index].role_status = role_status == 'true'
	res.status(200).send({ data: userslist[index], status: '200' })
})

app.get('/deluser', (req, res) => {
	const id = req.query.id - 0
	console.log(req.query)
	const index = userslist.findIndex(it => it.id == id)
	if (index == -1) {
		return res.status(200).send({ data: null, status: '500', msg: 'failed!'})
	}
	userslist.splice(index, 1)
	res.status(200).send({ data: null, status: '200', msg: 'done!' })
})

app.post('/adduser', (req, res) => {
	let { username, password, tel } = req.body
	let new_role = {
		id: userslist[userslist.length-1].id + 1,
		role_name: 'admin',
		username,
		tel,
		role_status: false
	}
	userslist.push(new_role)
	res.status(200).send({ data: new_role, status: '200' })
})

module.exports = app
